package com.fauks.ssm.mybatis;

import com.fauks.ssm.mybatis.bean.MStudent;
import com.fauks.ssm.mybatis.mapper.IStudenDao;
import com.fauks.ssm.mybatis.mapper.impl.StudentDaoImpl;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class _01MyBatisTestBasic {

    private IStudenDao studenDao;

    @Before
    public void initDao() {
        studenDao = new StudentDaoImpl();
    }

    //测试插入学生
    @Test
    public void testInsert() {
        studenDao.insert(new MStudent("slx", 35, 43.4, "不错", "李沧"));
    }

    //测试插入学生并返回id
    @Test
    public void testInsertGetId() {
        MStudent student;
        studenDao.insertAndGetId(student = new MStudent("马克西姆2", 45, 43.4));
        System.out.println("学生id=" + student.getId());
    }

    //测试删除学生
    @Test
    public void testDelete() {
        studenDao.deleteById(18);
    }

    //测试修改学生
    @Test
    public void testUpdate() {
        MStudent student = new MStudent("张学生", 12, 23.4);
        student.setId(20);
        studenDao.update(student);
    }

    //查询全部学生
    @Test
    public void testSelectAll() {
        List<MStudent> mStudents = studenDao.selectAllStudents();
        mStudents.stream().map(student -> student.getName())
                .forEach(System.out::println);
    }

    //查询全部学生返回map
    @Test
    public void testSelectMap() {
        Map<String, Object> stringObjectMap = studenDao.selectStudentsMap();
        for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
            String key = stringObjectEntry.getKey();
            Object value = stringObjectEntry.getValue();
            System.out.println("key=" + key + ",value=" + value);
        }
    }

    //根据id查询
    @Test
    public void testSelectById() {
        System.out.println(studenDao.selectStudentById(1));
    }


    //模糊查询
    @Test
    public void testSelectByName() {
        List<MStudent> mStudents = studenDao.selectStudentByName("克");
        mStudents.stream().map(student -> student.getName())
                .forEach(System.out::println);
    }

    //动态拼接搜索条件
    @Test
    public void testSearchByCondition() {
        MStudent mStudent = studenDao.selectStuByCondition("funNick", "35");
        System.out.println(mStudent);
    }

    @Test
    public void testUpdateByCondition() {
        MStudent mStudent = new MStudent();
        mStudent.setId(4);
        mStudent.setName("fauks");
        mStudent.setAge(100);
        studenDao.updateByCondition(mStudent);
    }
}
